package com.example.ucs_alg_node_manager.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ucs_alg_node_manager.domain.Assemble;
import com.example.ucs_alg_node_manager.service.AssembleService;
import com.example.ucs_alg_node_manager.mapper.AssembleMapper;
import com.example.ucs_alg_node_manager.vo.ReturnVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 31586
* @description 针对表【ucs_assemble】的数据库操作Service实现
* @createDate 2024-08-29 16:05:15
*/
@Service
public class AssembleServiceImpl extends ServiceImpl<AssembleMapper, Assemble>
    implements AssembleService{
    @Resource
    private AssembleMapper assembleMapper;

    @Override
    public ReturnVO getSources(String type, Integer id) {
        List<Assemble> list = list(Wrappers.<Assemble>lambdaQuery().eq(Assemble::getType, type)
                .eq(Assemble::getTypeId, id));
        return ReturnVO.ok(list).message("查询成功");
    }

    private boolean isExitsSource(Assemble assemble){
        Assemble one = getOne(Wrappers.<Assemble>lambdaQuery().eq(Assemble::getType, assemble.getType())
                .eq(Assemble::getTypeId, assemble.getTypeId())
                .eq(Assemble::getValue, assemble.getValue()));

        return one!=null;
    }

    @Override
    public ReturnVO editSource(Assemble assemble) {

        if (isExitsSource(assemble)) {
            return ReturnVO.fail().message("编辑失败");
        }
        if(assemble.getId()!=null) {
            updateById(assemble);
            return ReturnVO.ok().message("修改成功");
        }
        save(assemble);
        return ReturnVO.ok().message("添加成功");
    }

    @Override
    public ReturnVO deleteSource(Integer id) {
        removeById(id);
        return ReturnVO.ok().message("删除成功");
    }

    @Override
    public void removeSources(Integer typeId, String type) {
        remove(Wrappers.<Assemble>lambdaQuery().eq(Assemble::getType, type)
                .eq(Assemble::getTypeId, typeId));
    }
}




